package com.atzly.community.utils;

/**
 * @author zlysimida
 * @date 2021/8/30 - 16:15
 */
public class RedisKeyUtils {

        /**
         * Redis在分布式下的问题                                         解决方案
         * 1 缓存穿透(访问原本不存在的数据)                               添加空值  布隆过滤器
         * 2 缓存雪崩(redis中大量key过期)                                将过期时间分配开
         * 3 缓存击穿(大量请求同时访问一个redis中的key而此时key过期)        加锁(注意时序问题,需要保证原子性操作)
         */
        private static final String SPLIT = ":";
        private static final String PREFIX_ENTITY_LIKE = "like:entity";
        private static final String PREFIX_USER_LIKE = "like:user";

        private static final String FOLLOWEE_KEY = "followee";
        private static final String FOLLOWER_KYE = "follower";

        private static final String KAPTCHA_KEY = "kaptcha";
        private static final String LOGIN_TICKET = "ticket";
        private static final String USER_KEY = "user";

        // 独立访客
        private static final String UV_KEY = "uv";
        // 日常活跃用户
        private static final String DAU_KEY = "dau";

        // 帖子分数
        private static final String POST_KEY = "post";
        private static final String SCORE_KEY = "score";

        // 缓存热帖数据
        private static final String HOT_DATE_KEY = "hot:data";
        private static final String HOT_NUM_KEY = "hot:num";

        // 某个实体的赞
        // like:entity:entityType:entityId -> set(userId)
        public static String getEntityLikeKey(int entityType, int entityId) {
            return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
        }

        // 某个用户的赞
        // like:user:userId -> int
        public static String getUserLikeKey(int userId) {
            return PREFIX_USER_LIKE + SPLIT + userId;
        }

        // 某个用户关注的实体
        // followee:userId:entityType  -> zset(entityId,now)
        public static String getFolloweeKey(int userId,int entityType){
             return FOLLOWEE_KEY + SPLIT + userId + SPLIT + entityType ;
        }

        // 某个实体拥有的粉丝
        // follower:entityType:entityId -> zset(userId,now)
        public static String getFollowerKey(int entityType,int entityId){
                return FOLLOWER_KYE + SPLIT + entityType + SPLIT + entityId;

        }

        // 生成验证码key
        public static String getKaptchaKey(String suffix){
                return KAPTCHA_KEY + SPLIT + suffix;
        }

        // 生成登录凭证key
        public static String getLoginTicket(String ticket){
                return LOGIN_TICKET + SPLIT + ticket;
        }

        // 生成用户key
        public static String getUserKey(int userId){
                return USER_KEY + SPLIT + userId;
        }



        // 生成单日UV
        public static String getUvKey(String date){
                return UV_KEY + SPLIT + date;
        }

        // 生成区间UV
        public static String getUvKey(String startDate,String endDate){
                return UV_KEY + SPLIT + startDate + SPLIT + endDate;
        }

        // 生成DAU单日日常活跃用户
        public static String getDauKey(String date){
                return DAU_KEY + SPLIT + date;
        }

        // 生成区间日常活跃用户
        public static String getDauKey(String startDate,String endDate){
                return DAU_KEY + SPLIT + startDate + SPLIT +endDate;
        }
        // 生成帖子分数
        public static String getPostScoreKey(){
                return POST_KEY + SPLIT + SCORE_KEY;
        }

        // 生成相应的热帖key
        public static String getHotDateKey(int offset,int limit){
                return HOT_DATE_KEY + SPLIT + offset + SPLIT + limit;
        }

        // 生成相应的热帖数量key
        public static String getHotNumKey(int score){
                return HOT_NUM_KEY + SPLIT + score;
        }
}
